package com.ailianlian.ablecisi.viewmodel;

import android.app.Application;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.ailianlian.ablecisi.pojo.entity.AiCharacter;
import com.ailianlian.ablecisi.pojo.entity.User;
import com.ailianlian.ablecisi.utils.LoginInfoUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 个人资料页面的ViewModel
 */
public class ProfileViewModel extends AndroidViewModel {

    private MutableLiveData<User> userProfile = new MutableLiveData<>();
    private MutableLiveData<List<AiCharacter>> characters = new MutableLiveData<>();
    private MutableLiveData<Boolean> isLoading = new MutableLiveData<>(false);
    private Application application;

    public ProfileViewModel(Application application) {
        super(application);
        this.application = application;
        // 初始化时加载用户资料和角色列表
        loadUserProfile();
        loadCharacters();
    }

    public LiveData<User> getUserProfile() {
        return userProfile;
    }

    public LiveData<List<AiCharacter>> getCharacters() {
        return characters;
    }

    public LiveData<Boolean> getIsLoading() {
        return isLoading;
    }


    private String getCurrentUserId(Application application) {
        return LoginInfoUtil.getUserId(application);
    }

    private String getCurrentUserName(Application application) {
        return LoginInfoUtil.getUsername(application);
    }
    /**
     * 加载用户资料
     */
    public void loadUserProfile() {
        isLoading.setValue(true);
        // 从SharedPreferences获取用户ID


        User profile = new User(
                getCurrentUserId(application),
                getCurrentUserName(application),
                "AI爱好者，喜欢探索新技术",
                "https://images.pexels.com/photos/1036623/pexels-photo-1036623.jpeg",
                120,
                85,
                42
        );
        profile.setPostIds(new ArrayList<>() {{
            add(1L);
            add(2L);
            add(3L);
        }});
        
        // 延迟模拟网络请求
        new Thread(() -> {
            try {
                Thread.sleep(1000);
                userProfile.postValue(profile);
                isLoading.postValue(false);
            } catch (InterruptedException e) {
                e.printStackTrace();
                isLoading.postValue(false);
            }
        }).start();
    }

    /**
     * 加载用户的AI角色列表
     */
    public void loadCharacters() {
        isLoading.setValue(true);
        
        // 模拟网络请求，实际项目中应从API获取数据
        List<AiCharacter> aiCharacterList = new ArrayList<>();
        aiCharacterList.add(new AiCharacter("1", "小雪", "温柔女友", "https://images.pexels.com/photos/1036623/pexels-photo-1036623.jpeg", true));
        aiCharacterList.add(new AiCharacter("2", "小智", "学习助手", "https://images.pexels.com/photos/2379005/pexels-photo-2379005.jpeg", false));
        aiCharacterList.add(new AiCharacter("3", "小文", "创意伙伴", "https://images.pexels.com/photos/1239291/pexels-photo-1239291.jpeg", true));
        aiCharacterList.add(new AiCharacter("4", "小厨", "生活顾问", "https://images.pexels.com/photos/1681010/pexels-photo-1681010.jpeg", false));
        
        // 延迟模拟网络请求
        new Thread(() -> {
            try {
                Thread.sleep(1000);
                characters.postValue(aiCharacterList);
                isLoading.postValue(false);
            } catch (InterruptedException e) {
                e.printStackTrace();
                isLoading.postValue(false);
            }
        }).start();
    }

    /**
     * 编辑用户资料
     */
    public void editProfile(String username, String description) {
        // TODO: 保存资料到服务器
        isLoading.setValue(true);
        
        // 获取当前资料
        User currentProfile = userProfile.getValue();
        if (currentProfile != null) {
            // 更新资料
            currentProfile.setUsername(username);
            currentProfile.setDescription(description);
            
            // 模拟网络请求保存资料
            new Thread(() -> {
                try {
                    Thread.sleep(1000);
                    userProfile.postValue(currentProfile);
                    isLoading.postValue(false);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    isLoading.postValue(false);
                }
            }).start();
        } else {
            isLoading.setValue(false);
        }
    }
} 